package com.tonyodev.fetchapp.fetch2rx;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;

import com.tonyodev.fetch2.FetchConfiguration;
import com.tonyodev.fetch2.Request;
import com.tonyodev.fetch2rx.RxFetch;
import com.tonyodev.fetch2rx.RxFetchImpl;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;


public class RxFetchImplTest extends TestCase {
    RxFetch rxFetch;
    Context context;

    @Before
    public void setUp() throws Exception {
        context = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility().getContext();
        assertEquals("com.tonyodev.fetchapp", context.getBundleName());
        final FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(context).build();
        rxFetch = RxFetch.Impl.getRxInstance(fetchConfiguration);
        assertNotNull(rxFetch);
    }

    @After
    public void testClose() throws Exception {
        rxFetch.close();
        assertTrue(rxFetch.isClosed());
    }

    @Test
    public void testContext() {
        Request request = getTestRequest();
        rxFetch.enqueue(request);
    }


    public Request getTestRequest() {
        final String url = "http://www.example.com/test.txt";
        final String file = context.getFilesDir() + "/testFolder/test" + System.nanoTime() + ".txt";
        return new Request(url, file);
    }

    public List<Request> getTestRequestList(int size) {
        final ArrayList<Request> requests = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            final Request request = getTestRequest();
            requests.add(request);
        }
        return requests;
    }
}